package com.bulemobi.activity.ac_home_page;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.view.BaseActivity;
import com.jsyh.hcb.R;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by 武 on 2014/9/24 0024.
 * 测试页面
 */
public class Ac_honme_test extends BaseActivity implements View.OnClickListener {
    //放大缩小倍数
    private float scale = 1.5f;
    //动画时间
    private long animationTime = 2000;

    private RelativeLayout allView;

    private RelativeLayout yuan1;

    private AnimationSet animationSet;

    private TranslateAnimation translateAnimation;

    private ScaleAnimation scaleAnimation;

    private AnimatorSet set1 = new AnimatorSet();
    private AnimatorSet set2 = new AnimatorSet();
    private AnimatorSet set3 = new AnimatorSet();
    private AnimatorSet set4 = new AnimatorSet();
    private AnimatorSet set5 = new AnimatorSet();

    @ViewInject(id = R.id.yuan2)
    LinearLayout yuan2;
    @ViewInject(id = R.id.yuan3)
    LinearLayout yuan3;

    private boolean yuan1IsBig = false;

    private boolean yuan2IsBig = false;

    private boolean yuan3IsBig = false;

    private int startWidth;
//TODO:还有两个动画
//    private int

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        initView();
    }

    private void initView() {
        allView = (RelativeLayout) findViewById(R.id.allView);
        yuan1 = (RelativeLayout) findViewById(R.id.yuan1);
        yuan1.setOnClickListener(this);
        yuan2.setOnClickListener(this);
        yuan3.setOnClickListener(this);
    }


    /**
     * 获取allView的中心Y坐标
     * allView 是三个圆的父容器
     *
     * @return
     */
    private int getAllViewCentreY() {
        int allViewheight;
        allViewheight = (int) (allView.getY() + allView.getY() + allView.getHeight()) / 2;
        return allViewheight;
    }

    /**
     * 初始化动画
     */
    private void initAnimation() {
        startWidth = yuan1.getWidth();
        set1.playTogether(
                ObjectAnimator.ofFloat(yuan1, "translationX", 0, getWindowWidth() / 2 - yuan1.getX() - yuan1.getWidth() / 2),
                ObjectAnimator.ofFloat(yuan1, "translationY", 0, getAllViewCentreY() - allView.getY() - yuan1.getHeight() / 2),
                ObjectAnimator.ofFloat(yuan1, "scaleX", 1, 1.5f),
                ObjectAnimator.ofFloat(yuan1, "scaleY", 1, 1.5f)
        );
        Log.d("initAnimation", yuan1.getX() + "");
        set2.playTogether(
                ObjectAnimator.ofFloat(yuan2, "translationX", 0, -yuan2.getWidth() / 2 + getResources().getDimension(R.dimen.dim48)),
//                ObjectAnimator.ofFloat(yuan2, "translationY", 0, getAllViewCentreY()- allView.getY() - yuan1.getHeight() / 2),
                ObjectAnimator.ofFloat(yuan2, "scaleX", 1, 0.5f),
                ObjectAnimator.ofFloat(yuan2, "scaleY", 1, 0.5f)
        );
        set3.playTogether(
                ObjectAnimator.ofFloat(yuan3, "translationX", 0, yuan3.getWidth() / 2 - getResources().getDimension(R.dimen.dim48)),
                ObjectAnimator.ofFloat(yuan3, "translationY", 0, -getAllViewCentreY() + allView.getY() + yuan1.getHeight() / 2),
                ObjectAnimator.ofFloat(yuan3, "scaleX", 1, 0.5f),
                ObjectAnimator.ofFloat(yuan3, "scaleY", 1, 0.5f)
        );
    }

    private void initYuan2Animation() {
        set4.playTogether(
                ObjectAnimator.ofFloat(yuan2, "translationX", 0, getWindowWidth() / 2 - yuan2.getX() - yuan2.getWidth()),
//                ObjectAnimator.ofFloat(yuan2, "translationY", 0, -getAllViewCentreY() + allView.getY() + yuan1.getHeight() / 2),
                ObjectAnimator.ofFloat(yuan2, "scaleX", 0.5f, 1.5f),
                ObjectAnimator.ofFloat(yuan2, "scaleY", 0.5f, 1.5f)
        );
    }

    private int getWindowHeight() {
        WindowManager wm = this.getWindowManager();
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yuan1:
                if (yuan1IsBig) {

                } else {
                    initAnimation();
                    set1.setDuration(animationTime).start();
                    set2.setDuration(animationTime).start();
                    set3.setDuration(animationTime).start();
                    yuan1IsBig = true;
                }
                break;
            case R.id.yuan2:
                if (yuan2IsBig) {

                } else {
                    initYuan2Animation();
                    set4.setDuration(animationTime).start();
                    yuan2IsBig = true;
                }
                break;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
//        initAnimation();
        super.onWindowFocusChanged(hasFocus);
    }

    private int getWindowWidth() {
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }
}
